package com.hinnotek.amoeba.ui.activity.old

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.base.BaseBean
import com.hinnotek.amoeba.bean.old.AmeobaCustomerListBean
import com.hinnotek.amoeba.bean.old.AmibaProjectListBean
import com.hinnotek.amoeba.bean.old.AmountAllocatedBean
import com.hinnotek.amoeba.databinding.ActivityRDCostLocationBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.utilities.ClickUtil
import com.lee.common.utils.SharedPreUtils
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.ext.initAdapter
import com.hinnotek.amoeba.viewmodels.ManageViewModel
import com.hinnotek.amoeba.viewmodels.ProjectViewModel
import com.hinnotek.amoeba.viewmodels.PublicInterfaceViewModel
import com.lee.common.network.getRequestBody
import com.lee.common.network.net.ObserverState
import com.xuexiang.xutil.common.StringUtils
import dagger.hilt.android.AndroidEntryPoint
import java.util.*

@AndroidEntryPoint
class RdCostAllocationActivity : BaseActivity<ActivityRDCostLocationBinding>() {

    private var data1: AmibaProjectListBean ? = null
    private var rd_orgId = 0
    private var productId = 0
    private var orgId = 0
    private var customerId = 0
    private var amibaCustomerProjectId = 0


    val viewModel by viewModels<ProjectViewModel>()
    val manageViewModel by viewModels<ManageViewModel>()
    val publicViewModel by viewModels<PublicInterfaceViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_r_d_cost_location)
        setLeftVisible(true)
        setTitle("研发成本分摊")

    }

    override fun initVew() {

        bindingView.superButtonSubmit.setOnClickListener { v: View? ->
            if (ClickUtil.isFastClick()) {
                return@setOnClickListener
            }
            val escRdAmoebaS =
                bindingView.escRdAmoeba.editText.getText().toString().trim()
            if (StringUtils.isEmpty(escRdAmoebaS)) {
                ToastUtils.show(mContext, "请选择研发阿米巴")
                return@setOnClickListener
            }
            val escProductS =
                bindingView.escProduct.editText.getText().toString().trim()
            if (StringUtils.isEmpty(escProductS)) {
                ToastUtils.show(mContext, "请选择研发产品")
                return@setOnClickListener
            }
            val orgName =
                bindingView.escAmoeba.editText.getText().toString().trim()
            if (StringUtils.isEmpty(orgName)) {
                ToastUtils.show(mContext, "请选择阿米巴")
                return@setOnClickListener
            }
            val escCustomerS =
                bindingView.escAmoeba.editText.getText().toString().trim()
            if (StringUtils.isEmpty(escCustomerS)) {
                ToastUtils.show(mContext, "请选择客户")
                return@setOnClickListener
            }
            val escPjS = bindingView.escPj.editText.text.toString().trim()
            if (StringUtils.isEmpty(escPjS)) {
                ToastUtils.show(mContext, "请选择项目")
                return@setOnClickListener
            }
            val etRDS = bindingView.etRD.text.toString().trim()
            if (StringUtils.isEmpty(etRDS)) {
                ToastUtils.show(mContext, "请填写分摊金额")
                return@setOnClickListener
            }
            if (etRDS.toDouble() > data1!!.surplusMoney) {
                ToastUtils.show(mContext, "剩余金额不足！")
                return@setOnClickListener
            }
            addCompanyProjectDistribution(
                orgId, orgName, customerId, escCustomerS, amibaCustomerProjectId,
                escPjS, rd_orgId, escRdAmoebaS, productId, escProductS, etRDS
            )
        }
    }

    override fun setData() {
        getSysOrgAmibaList()
    }

    /**
     * app端添加研发项目成本分配记录接口
     */
    private fun addCompanyProjectDistribution(
        orgId: Int,
        orgName: String,
        customerId: Int,
        customerName: String,
        amibaCustomerProjectId: Int,
        projectName: String,
        provideOrgId: Int,
        provideOrgName: String,
        amibaProjectId: Int,
        provideProjectName: String,
        divideMoney: String
    ) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()
        val map = mutableMapOf<String,Any>().apply {
            put("orgId", orgId)
            put("orgName", orgName)
            put("customerId", customerId)
            put("customerName", customerName)
            put("amibaCustomerProjectId", amibaCustomerProjectId)
            put("projectName", projectName)
            put("provideOrgId", provideOrgId)
            put("provideOrgName", provideOrgName)
            put("amibaProjectId", amibaProjectId)
            put("provideProjectName", provideProjectName)
            put("divideMoney", divideMoney)
            put("createUser", SharedPreUtils.getString("userName"))
        }
        viewModel.addCompanyProjectDistribution(getRequestBody(map)).observe(this,object : ObserverState<BaseBean>(){
            override fun onDataChange(data: BaseBean) {
                customDialog.dismiss()
                finishActivity()
                super.onDataChange(data)
            }

            override fun onError(msg: String?, code: Int) {
                customDialog.dismiss()
                ToastUtils.showShort(mContext, msg)
                super.onError(msg, code)
            }
        })
    }

    /**
     * app端获取全部阿米巴组织接口
     */
    private fun getSysOrgAmibaList(){
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()

        manageViewModel.getSysOrgAmibaList().observe(this,object : ObserverState<MutableList<AmountAllocatedBean>>(){
            override fun onDataChange(data: MutableList<AmountAllocatedBean>) {
                val amountAllocatedTypeList: MutableList<String?> = ArrayList()
                for (index in data) {
                    amountAllocatedTypeList.add(index.orgName)
                }
                val strings = arrayOfNulls<String>(amountAllocatedTypeList.size)
                bindingView.escRdAmoeba.initAdapter(strings)
                bindingView.escRdAmoeba.setOnItemClickListener { parent, view, position, id ->
                    rd_orgId = data[position].orgId

                    publicViewModel.getAmibaProjectListByOrgId(rd_orgId).observe(this@RdCostAllocationActivity,object : ObserverState<MutableList<AmibaProjectListBean>>(){
                        override fun onDataChange(data: MutableList<AmibaProjectListBean>) {
                            val stringArrays = arrayOfNulls<String>(data.size).apply {
                                for (index in data.indices){
                                    this[index] = data[index].projectName
                                }
                            }
                            bindingView.escProduct.initAdapter(stringArrays)
                            bindingView.escProduct.setOnItemClickListener { parent, view, position, id ->
                                data1 = data[position]
                                productId = data1!!.amibaProjectId
                                bindingView.llTv.visibility = View.VISIBLE
                                bindingView.tv1.text = "剩余金额：" + data1!!.surplusMoney
                            }

                            super.onDataChange(data)
                        }
                    })
                }
                bindingView.escAmoeba.initAdapter(strings)
                bindingView.escAmoeba.setOnItemClickListener { _, _, position, id ->
                    orgId = data[position].orgId

                    publicViewModel.getAmibaCustomerListByOrgId(orgId).observe(this@RdCostAllocationActivity,object : ObserverState<MutableList<AmeobaCustomerListBean>>(){

                        override fun onDataChange(data: MutableList<AmeobaCustomerListBean>) {
                            val stringArrays = arrayOfNulls<String>(data.size).apply {
                                for (index in data.indices){
                                    this[index] = data[index].customerName
                                }
                            }
                            bindingView.escCustomer.initAdapter(stringArrays)
                            bindingView.escCustomer.setOnItemClickListener { _, _, position, _ ->
                                amibaCustomerProjectId = data[position].customerId
                            }

                            super.onDataChange(data)
                        }

                    })
                }
                super.onDataChange(data)
            }
        })

    }

}